/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"github.com/rs/zerolog"
	"github.com/rs/zerolog/log"
	"github.com/spf13/cobra"
	"os"
	"oss-bench/app/s3"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
	Use:   "oss-bench",
	Short: "one tools",
	Long:  ``,
	// Uncomment the following line if your bare application
	// has an action associated with it:
	Run: func(cmd *cobra.Command, args []string) {
		if !verbose {
			log.Logger = log.Logger.Level(zerolog.InfoLevel)
		}
		if ao.EndpointUrl == "" {
			cmd.Help()
			return
		}
		s := s3.NewS3Storage(ao, bo)
		err := s.Run()
		if err != nil {
			log.Err(err).Send()
			return
		}
	},
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
	err := rootCmd.Execute()
	if err != nil {
		os.Exit(1)
	}
}

var ao = new(s3.AccessOptions)
var bo = new(s3.BenchOptions)
var verbose = false

func init() {
	rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "print verbose")

	rootCmd.Flags().StringVarP(&ao.EndpointUrl, "endpoint", "e", "", "the endpoint of s3")
	rootCmd.Flags().StringVarP(&ao.AccessKey, "access-key", "a", "", "the accessKey of s3")
	rootCmd.Flags().StringVarP(&ao.SecretKey, "secret-key", "s", "", "the secretKey of s3")
	rootCmd.Flags().StringVarP(&ao.Bucket, "bucket", "b", "", "the bucket of s3")
	rootCmd.Flags().StringVarP(&ao.Region, "region", "r", "", "the region of s3")

	rootCmd.Flags().BoolVarP(&bo.PutEnable, "put-enable", "", false, "enable put")
	rootCmd.Flags().StringVarP(&bo.PutPrefix, "put-path", "", "", "specified put prefix")
	rootCmd.Flags().IntVarP(&bo.PutNum, "put-num", "", 10000, "specified put objects num")
	rootCmd.Flags().IntVarP(&bo.PutConcurrent, "put-concurrent", "", 20, "specified put objects concurrent")
	rootCmd.Flags().IntVarP(&bo.PutSize, "put-size", "", 10, "specified put objects size, unit is B")

	rootCmd.Flags().BoolVarP(&bo.DeleteEnable, "delete-enable", "", false, "enable delete")
	rootCmd.Flags().StringVarP(&bo.DeletePrefix, "delete-prefix", "", "", "the prefix of s3 objects want delete")
	rootCmd.Flags().IntVarP(&bo.DeleteBatchSize, "delete-batch", "", 1000, "the delete batch size in one request")
	rootCmd.Flags().IntVarP(&bo.DeleteConcurrent, "delete-concurrent", "", 0, "specified delete objects concurrent")
	rootCmd.Flags().BoolVarP(&bo.DeleteFollow, "delete-follow", "", false, "specified delete objects follow get patch, delete-concurrent will disable if specified")

}
